package com.td.module.farmer.controller.admin.consultation;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.farmer.controller.admin.consultation.vo.*;
import com.td.module.farmer.convert.consultation.ConsultationConvert;
import com.td.module.farmer.dal.dataobject.consultation.ConsultationDO;
import com.td.module.farmer.service.consultation.ConsultationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 农事咨询")
@RestController
@RequestMapping("/farmer/consultation")
@Validated
public class ConsultationController {

    @Resource
    private ConsultationService consultationService;

    @PostMapping("/create")
    @ApiOperation("创建农事咨询")
    @PreAuthorize("@ss.hasPermission('farmer:consultation:create')")
    public CommonResult<Long> createConsultation(@Valid @RequestBody ConsultationCreateReqVO createReqVO) {
        return success(consultationService.createConsultation(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新农事咨询")
    @PreAuthorize("@ss.hasPermission('farmer:consultation:update')")
    public CommonResult<Boolean> updateConsultation(@Valid @RequestBody ConsultationUpdateReqVO updateReqVO) {
        consultationService.updateConsultation(updateReqVO);
        updateReqVO.setWhetherView(1);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除农事咨询")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('farmer:consultation:delete')")
    public CommonResult<Boolean> deleteConsultation(@RequestParam("id") Long id) {
        consultationService.deleteConsultation(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得农事咨询")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('farmer:consultation:query')")
    public CommonResult<ConsultationRespVO> getConsultation(@RequestParam("id") Long id) {
        ConsultationDO consultation = consultationService.getConsultation(id);
        return success(ConsultationConvert.INSTANCE.convert(consultation));
    }

    @GetMapping("/list")
    @ApiOperation("获得农事咨询列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('farmer:consultation:query')")
    public CommonResult<List<ConsultationRespVO>> getConsultationList(@RequestParam("ids") Collection<Long> ids) {
        List<ConsultationDO> list = consultationService.getConsultationList(ids);
        return success(ConsultationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得农事咨询分页")
    @PreAuthorize("@ss.hasPermission('farmer:consultation:query')")
    public CommonResult<PageResult<ConsultationRespVO>> getConsultationPage(@Valid ConsultationPageReqVO pageVO) {
        PageResult<ConsultationDO> pageResult = consultationService.getConsultationPage(pageVO);
        return success(ConsultationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出农事咨询 Excel")
    @PreAuthorize("@ss.hasPermission('farmer:consultation:export')")
    @OperateLog(type = EXPORT)
    public void exportConsultationExcel(@Valid ConsultationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ConsultationDO> list = consultationService.getConsultationList(exportReqVO);
        // 导出 Excel
        List<ConsultationExcelVO> datas = ConsultationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "农事咨询.xls", "数据", ConsultationExcelVO.class, datas);
    }

}
